<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!-- 业务员选择 -->
 <div class="content-show" id="searchDiv" style="display: none;">
 	<div class="searchtop">
	<input type="text"  class="form-control smallicon  fixedsearch"  id="searchname" name="searchname" placeholder="业务员姓名" onkeyup="salemancho(this.value, true);" onafterpaste="salemancho(this.value, true);">
	</div>
	<ul class="cho-ul" id="searchUl"></ul>
</div>

<!-- 业务员选择-->
<script type="text/javascript">
 var exete = true;
 function salemancho(name,hasOpen){
    if(exete){
    	exete = false;
    	setTimeout(function(){
    		exete = true;
 		}, 500);
    	searchSaleman(name,hasOpen);
    } else {
	    setTimeout(function(){
    		if(exete) { //超过500ms还没有触发，则再补充执行最后一次
    			searchSaleman(name, true);
    		}
 		}, 500);
    }
  }
 
 function removeDuan(){
	 var minZindex = 0;
		var $shadeDiv;
		$('.layui-layer-shade').each(function(i,obj){
			var thisIndex = $(this).css('z-index');
			if(i == 0) {
				minZindex = thisIndex;
				$shadeDiv = $(this);
			} else {
				if(minZindex > thisIndex) {
					$shadeDiv.remove();
					$shadeDiv = $(this);
					minZindex = thisIndex;
				} else {
					$(this).remove();
				}
			}
		});
		$('.layui-layer-move').each(function(i,obj){
			$(this).remove();
		});
 }

 var layerIndex = 0;
 
 function searchSaleman(name,hasOpen) {
	  var name = name || '';
   	  $.dpAjax({
   			show:false,
    		url:'${basePath}/YwShenQingZxController/getBaseSalersman',
    		data:{name:name,row:10},
			success: function(datas){
			 	  if(datas.flag == "100100"){
			 		var records = datas.result;
			 		$searchUl = $('#searchUl');
			 		$searchUl.empty();
			 		if(records && records.length > 0) {
			 			var content = '';
				 		for(var i=0;i< records.length; i++){
				 			var record = records[i];
			 				content +='<a href="javascript:choice(\''+record.replaceId+'\', \''+record.name+'\');"><li><span class="li-name"><lable>姓名:</lable>' + record.name +'('+ record.orgName +')</span><lable>编号:</lable>' + record.salersmanCode +'</li></a>';  
				 		}
				 		$searchUl.html(content);
			 		}
			 		if(!hasOpen) {
			 			$("#searchname").val(name);
			 			layerIndex = layer.open({
						  title:'业务员选择',
						  type: 1,
						  area: ['580px', '500px'], //宽高
						  content: $('#searchDiv')
						}); 
			 		}
			 		 removeDuan();
			 		$("#searchname").focus();
					return;
				  }
				  if(datas.flag == "100200"){
					  alert(JSON.stringify(datas));
                	  return;
                  }
				  layer.msg(datas.message, {icon: 2});
             }
    	 });
    }
	var choice = function(id, name, orgCode, orgName)
	{
		document.getElementById("salemanName").value = name;
		document.getElementById("salemanId").value = id;
		layer.close(layerIndex);
	}
</script>